import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/ui/medicine/treatment_sheet_detail_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/medecine/treatment_sheet_list_view_model.dart';

import '../../widgets/common_widget.dart';
class TreatmentSheetListPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _TreatmentSheetListState();

}

class _TreatmentSheetListState extends BaseMvvmListState<TreatmentSheetListViewModel,TreatmentSheetListPage>{

  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      key: key,
      appBar: AppBar(
        title: Text('治疗单'),
        actions: [IconButton(onPressed: (){
          key.currentState?.openEndDrawer();
        }, icon: Icon(Icons.search))],
      ),
      body: loadingContentWidget,
      endDrawer: endDraw(),
    );
  }



  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      alignment: Alignment.center,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                CommonText('${item.customerName}',textSize: 17,fontWeight: FontWeight.bold),
                SizedBox(width: 10,),
                CommonText('${item.bedNo??""}',textSize: 15),
                SizedBox().setWeight(1),
                // CommonText('处方号：CF6165522',fontWeight: FontWeight.bold)
              ],
            ),

            CommonText('开单日期：${item.prescriptionDate??""}'),
            CommonText('治疗单号：${item.prescriptionNo??""}'),
            Text.rich(TextSpan(
              text: '执行状态：',style: TextStyle(color: Colors.black,fontSize: 16),
              children: [
                TextSpan(
                  text: '${item.statusName}',
                  style: TextStyle(fontSize: 17,color: Colors.blue,fontWeight: FontWeight.bold)
                )
              ]
            )),
            CommonText('治疗总数量：${item.quantity?.d}'),
            CommonText('已执行数量：${item.completeQuantity?.d??0}'),
          ].insertWidget(SizedBox(height: 10,)),
        ),
        Icon(Icons.arrow_forward_ios).setLocation(right: 0),
        Column(
          children: [
            CommonText('项目名称'),
            SizedBox(height: 15,),
            CommonText('${item.projectName??""}',fontWeight: FontWeight.bold,textColor: Colors.blue,textSize: 18),
            SizedBox(height: 16,),
            if(item.status!=2)
            TextButton(onPressed: (){
              viewModel.handleClick(item);
            }, child: CommonText('执行',textColor: Colors.white),
            style: ButtonStyle(
              backgroundColor: WidgetStateProperty.all(Colors.blue),
              shape: WidgetStateProperty.all(StadiumBorder())
            ),)
          ],
        ).setLocation(right: 40,top: 10)
      ],
    ).onTap(() {
      Go().push(TreatmentSheetDetailPage(item.id));
    }).addToContainer(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: 6.borderRadius
      ),
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 10)
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入长者姓名',
                text: vm?.searchName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.searchName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 10,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: vm?.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),


            SizedBox(height: 10,),

            CommonText('项目名称'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '项目名称',
                text: vm?.itemName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.itemName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('开单日期'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.orderDate,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseOrderDate();
            }),

            SizedBox(height: 10,),

            CommonText('治疗单号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入治疗单号',
                text: vm?.treatmentCode ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.treatmentCode = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('执行状态'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.handleStatus?.pickName??"",hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseStatus();
            }),
            SizedBox(height: 10,),




          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(height: 10,),
        Row(
          children: [
            SizedBox(width: 16,),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',textColor: Colors.blue),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(left: 20.radius),
                      side: BorderSide(color: Colors.blue)
                  ))
              ),)
                .setWeight(1),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定',textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius),
                      side: BorderSide(color: Colors.blue),
                    ))
                ))
                .setWeight(1),
            SizedBox(width: 16,),
          ],
        ),
        SizedBox(height: 12,)
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  TreatmentSheetListViewModel createViewModel() {
    return TreatmentSheetListViewModel(context);
  }

  @override
  EdgeInsets get listPadding => EdgeInsets.symmetric(horizontal: 16,vertical: 12);

  @override
  Widget get separatorDivider => SizedBox(height: 12,);

  @override
  bool get canPullUp => true;
}